package dogredis

import (
  "fmt"
  "github.com/garyburd/redigo/redis"
  "time"
  "strconv"
)

var (
  // server string = "123.57.54.194:6379"
  server string = "127.0.0.1:6379"
  password string = "password"
)

var pool *redis.Pool

type RedisCmd struct {
}

type RedisStruct struct {
    pool *redis.Pool
}

func ( redisStru *RedisStruct ) SetString(key string , value string, timeout uint32) {
    c := redisStru.pool.Get()
    defer c.Close()
    c.Do("SETEX", key, timeout, value)
}

// redis 删除string了类型的数据
func (redisStru *RedisStruct) DelStringType(key string) (value string, flag bool) {
    c := redisStru.pool.Get()
    defer c.Close()
    if value, err := redis.String(c.Do("DEL", key)); err == nil {
      fmt.Println("return value =", value)
      flag = true
    } else {
      fmt.Println(err)
      flag = false
    }
    return
}

func (redisStru *RedisStruct) GetStringType(key string) (value string) {
    c := redisStru.pool.Get()
    defer c.Close()
    value, err := redis.String(c.Do("GET", key))
    if err == nil {
      fmt.Println("return value =", value)
    } else {
      fmt.Println(err)
    }

    return
}

func test(i int) {
  c := pool.Get()
  defer c.Close()

	t:=strconv.Itoa(i)
	c.Do("SETEX","foo"+t,20,i)

	reply, err := redis.Int(c.Do("GET","foo"+t))
	if err == nil {
		fmt.Print(reply)
	} else {
		fmt.Print(err)
	}
	time.Sleep(1*time.Second)
}

var Redis *RedisStruct = func() ( r *RedisStruct ) {
    r = new (RedisStruct)
    r.pool = &redis.Pool {
        MaxIdle     : 3000,                            /* 连接数 */
        IdleTimeout : 240 * time.Second,               /* 连接超时时间 */
        Dial        : func () (redis.Conn , error) {
            c , err  := redis.Dial("tcp", server)
            if err != nil {
                return nil, err
            }
        /*  redis如果设置密码
        if _, err := c.Do("AUTH", password); err != nil {
            c.Close()
            return nil,err
        }
        */
            return c, err
        },

        TestOnBorrow : func (c redis.Conn , t time.Time) error {
            _, err := c.Do("PING")
            return err
        },
    }

    return
} ()


func RedisCmdFactory() (redisCmd *RedisStruct){
  redisCmd = new (RedisStruct)
  redisCmd.pool = &redis.Pool {
    MaxIdle : 3,
    IdleTimeout : 240 * time.Second,
    Dial :func () (redis.Conn , error) {
      c , err := redis.Dial("tcp", server)
      if err != nil {
        return nil, err
      }
      /*  redis如果设置密码
      if _, err := c.Do("AUTH", password); err != nil {
        c.Close()
        return nil,err
      }
      */
      return c, err
    },

    TestOnBorrow : func (c redis.Conn , t time.Time) error {
      _, err := c.Do("PING")
      return err
    },
  }


  return
}

/* 作废 */
func poolInit() (*redis.Pool) {
  return &redis.Pool {
    MaxIdle : 3,
    IdleTimeout : 240 * time.Second,
    Dial :func () (redis.Conn , error) {
      c , err := redis.Dial("tcp", server)
      if err != nil {
        return nil, err
      }

/*
      if _, err := c.Do("AUTH", password); err != nil {
        c.Close()
        return nil,err
      }
      */

      return c, err
    },

    TestOnBorrow : func (c redis.Conn , t time.Time) error {
      _, err := c.Do("PING")
      return err
    },
  }
}
